﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExamV5.StudyInfo
{
    public partial class NewSunjectManagerZJEdit : System.Web.UI.Page
    {
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private int int_0 = 0;
        private int int_1 = 0;
        private PublicFun publicFun_0 = new PublicFun();
        public string ItemContent = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if (!((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false))
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
            else
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                base.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString + "---编辑教材章节";
                this.int_1 = Convert.ToInt32(base.Request["ID"].ToString());
                this.int_0 = Convert.ToInt32(base.Request["BookID"].ToString());
                if (Convert.ToInt32(ConfigurationManager.ConnectionStrings["OnlineNum"].ConnectionString) != 0x3a)
                {
                    if (!base.IsPostBack)
                    {
                        if (this.publicFun_0.ReturnPaperData() != "Yes")
                        {
                            base.Response.Write("<script>alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')</script>");
                        }
                        else
                        {
                            this.labBookName1.Text = this.labBookName.Text = this.publicFun_0.GetValues("select SubjectName from BookInfo where ID=" + this.int_0, "SubjectName");
                            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                            connection.Open();
                            SqlCommand command = new SqlCommand("select AddText,ManagText from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                            if (reader.Read())
                            {
                                this.ViewState["AddPower"] = reader["AddText"].ToString();
                                this.ViewState["ManagPower"] = reader["ManagText"].ToString();
                            }
                            reader.Close();
                            command.Dispose();
                            connection.Close();
                            connection.Dispose();
                            if ((this.ViewState["AddPower"].ToString() != "Y") ? !(this.ViewState["ManagPower"].ToString() != "Y") : true)
                            {
                                this.publicFun_0.GetItem(this.DDLUPItem, this.int_0);
                                ListItem item = new ListItem("--顶层章节--", "0");
                                this.DDLUPItem.Items.Insert(0, item);
                                this.DDLUPItem.SelectedIndex = -1;
                                this.DDLUPItem.Items.FindByValue("0").Selected = true;
                                if (this.int_1 == 0)
                                {
                                    this.butDel.Attributes.Add("onclick", "javascript:{if(confirm('确定要新增教材章节信息吗？')==false) return false;}");
                                    this.butDel.Visible = false;
                                }
                                else
                                {
                                    this.butDefault.Attributes.Add("onclick", "javascript:{if(confirm('确定要修改教材章节信息吗？')==false) return false;}");
                                    this.butDel.Attributes.Add("onclick", "javascript:{if(confirm('确定要删除教材章节信息吗？')==false) return false;}");
                                    this.method_1();
                                    this.butDel.Visible = true;
                                }
                                this.TreeZJ.Nodes.Clear();
                                TreeNode node = new TreeNode();
                                this.method_0(this.labBookName.Text, node, this.int_0.ToString(), "0");
                            }
                            else
                            {
                                base.Response.Write("<script>alert('添加或管理电子教材权限未开放！')</script>");
                                this.butDefault.Visible = false;
                            }
                        }
                    }
                }
                else
                {
                    base.Response.Write("<script>alert('该版本为特惠版，此功能未开放！')</script>");
                }
            }
        }

        private void method_0(string string_0, TreeNode treeNode_0, string string_1, string string_2)
        {
            TreeNode node;
            node = new TreeNode
            {
                Value = string_2,
                Text = string_0,
                ToolTip = string_2,
                NavigateUrl = "NewSunjectManagerZJEdit.aspx?BookID=" + this.int_0.ToString() + "&ID=" + string_1
            };
            if (!(string_2 == "0"))
            {
                treeNode_0.ChildNodes.Add(node);
                treeNode_0.Expanded = true;
            }
            else
            {
                this.TreeZJ.Nodes.Add(node);
                treeNode_0.Expanded = true;
            }
            string cmdText = string.Concat(new object[] { "select ID,ItemName,UPID from BookItem where SubjectID=", this.int_0, " and UPID='", string_2, "' order by SortInfo,id " });
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlCommand command = new SqlCommand(cmdText, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                this.method_0(reader["ItemName"].ToString(), node, reader["ID"].ToString(), reader["ID"].ToString());
            }
            reader.Close();
            connection.Close();
            connection.Dispose();
        }

        private void method_1()
        {
            if (this.int_1 != 0)
            {
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                connection.Open();
                SqlCommand command = new SqlCommand(string.Concat(new object[] { "select * from BookItem where SubjectID=", this.int_0, " and ID=", this.int_1 }), connection);
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.Read())
                {
                    this.txtItemTitle.Text = reader["ItemName"].ToString();
                    this.txtSortInfo.Text = reader["SortInfo"].ToString();
                    this.txtFen.Text = reader["FenValue"].ToString();
                    //this.txtItemDoc.Value = reader["ItemContent"].ToString();
                    this.ItemContent = reader["ItemContent"].ToString();
                    this.DDLUPItem.SelectedIndex = -1;
                    this.DDLUPItem.Items.FindByValue(reader["UPID"].ToString()).Selected = true;
                }
                reader.Close();
                command.Dispose();
                connection.Close();
                connection.Dispose();
            }
        }

        protected void butDel_Click(object sender, EventArgs e)
        {
            if (this.publicFun_0.GetValues("select ID from BookItem where UPID=" + this.int_1, "ID") != "")
            {
                base.Response.Write("<script>alert('此章节下存在子章节,无法删除！')</script>");
            }
            else
            {
                this.publicFun_0.ExecuteSql(string.Concat(new object[] { "delete from BookItem where ID=", this.int_1, ";insert into SysLogInfo(LogDisp,EditUserCode,EditUser) values('删除教材章节信息！','", this.httpCookie_0.Value, "','", this.httpCookie_1.Value, "')" }));
                base.Response.Write("<script>alert('删除章节信息操作成功！');window.location.href='NewSunjectManagerZJEdit.aspx?BookID=" + this.int_0.ToString() + "&ID=0'</script>");
                this.txtItemTitle.Text = "";
                this.txtSortInfo.Text = "1";
                this.txtFen.Text = "0";
                //this.txtItemDoc.Value = "";
                this.DDLUPItem.SelectedIndex = -1;
                this.DDLUPItem.Items.FindByValue("0").Selected = true;
            }
        }

        protected void butDefault_Click(object sender, EventArgs e)
        {
            var content = this.Request.Form["editor"];
            string strSql = "";
            if (this.int_1 == 0)
            {
                strSql = string.Concat(new object[] { "insert into BookItem(FenValue,ItemName,SubjectID,UPID,InputUser,SortInfo) values('", this.txtFen.Text.Trim(), "','", this.txtItemTitle.Text, "',", this.int_0, ",", this.DDLUPItem.SelectedItem.Value, ",'", this.httpCookie_0.Value, "','", this.txtSortInfo.Text.Trim(), "')" });
                this.int_1 = this.publicFun_0.RetrunExecute(strSql);
            }
            else
            {
                if (this.int_1.ToString() == this.DDLUPItem.SelectedItem.Value)
                {
                    base.Response.Write("<script>alert('上级章节名称不可以为当前章节名称！');window.location.href='NewSunjectManagerZJEdit.aspx?BookID=" + this.int_0.ToString() + "&ID=0'</script>");
                    return;
                }
                strSql = string.Concat(new object[] { "update BookItem set FenValue='", this.txtFen.Text.Trim(), "',ItemName='", this.txtItemTitle.Text, "',SubjectID=", this.int_0, ",UPID=", this.DDLUPItem.SelectedItem.Value, ",InputUser='", this.httpCookie_0.Value, "',SortInfo='", this.txtSortInfo.Text.Trim(), "' where ID=", this.int_1 });
                this.publicFun_0.ExecuteSql(strSql);
            }
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("update BookItem set ItemContent=@ItemContent where ID=" + this.int_1, connection);
            command.Parameters.Add("@ItemContent", SqlDbType.Text).Value = content;
            command.ExecuteNonQuery();
            command.Dispose();
            connection.Close();
            connection.Dispose();
            this.txtItemTitle.Text = "";
            this.txtSortInfo.Text = "1";
            this.txtFen.Text = "0";
            //this.txtItemDoc.Value = "";
            this.DDLUPItem.SelectedIndex = -1;
            this.DDLUPItem.Items.FindByValue("0").Selected = true;
            base.Response.Write("<script>alert('提交章节信息操作成功！');window.location.href='NewSunjectManagerZJEdit.aspx?BookID=" + this.int_0.ToString() + "&ID=0'</script>");
        }
    }
}